function convert(input) { alert('Error. Contact us if this keeps happening.'); document.getElementById('modal-contact').classList.add('show'); } window.ctMaxUploadFiles = 100; var _ctI2GQueue = []; var _ctI2GTimer = null; function _ctI2GLoadImage(blob) { return new Promise(function(resolve, reject) { var url = URL.createObjectURL(blob); var img = new Image(); img.onload = function() { resolve({ image: img, url: url }); }; img.onerror = function() { URL.revokeObjectURL(url); reject(new Error('Could not decode image')); }; img.src = url; }); } function _ctI2GOptions() { var delayEl = document.getElementById('i2g-delay'); var loopEl = document.getElementById('i2g-loop'); var widthEl = document.getElementById('i2g-width'); var d = delayEl ? parseInt(delayEl.value, 10) : 250; var l = loopEl ? parseInt(loopEl.value, 10) : 0; var w = widthEl ? parseInt(widthEl.value, 10) : 0; if (!isFinite(d)) d = 250; d = Math.max(20, Math.min(5000, d)); if (!isFinite(l)) l = 0; l = Math.max(0, Math.min(100, l)); if (!isFinite(w)) w = 0; if (w > 0) w = Math.max(40, Math.min(2000, w)); return { delay: d, loop: l, maxWidth: w }; } function _ctI2GRunBatch(items) { if (!items || !items.length) return; var status = document.getElementById('i2g-status'); if (status) status.textContent = 'Loading ' + items.length + ' image' + (items.length > 1 ? 's' : '') + '...'; Promise.all([ loadScriptPromise('https://cdn.jsdelivr.net/npm/gif.js@0.2.0/dist/gif.js'), Promise.all(items.map(function(it){ return _ctI2GLoadImage(it.blob); })) ]).then(function(arr) { var loaded = arr[1]; if (typeof GIF === 'undefined') throw new Error('GIF library failed to load'); var opts = _ctI2GOptions(); // Determine output dimensions: use first frame's size, optionally scaled down to maxWidth. var w = loaded[0].image.naturalWidth; var h = loaded[0].image.naturalHeight; if (opts.maxWidth > 0 && w > opts.maxWidth) { var scale = opts.maxWidth / w; w = opts.maxWidth; h = Math.max(1, Math.round(h * scale)); } var gif = new GIF({ workers: 2, quality: 10, width: w, height: h, repeat: opts.loop === 0 ? 0 : opts.loop, workerScript: 'https://cdn.jsdelivr.net/npm/gif.js@0.2.0/dist/gif.worker.js' }); for (var i = 0; i < loaded.length; i++) { var c = document.createElement('canvas'); c.width = w; c.height = h; var ctx = c.getContext('2d'); ctx.fillStyle = '#ffffff'; ctx.fillRect(0, 0, w, h); // Fit image into the frame preserving aspect ratio (letterbox). var src = loaded[i].image; var srcW = src.naturalWidth, srcH = src.naturalHeight; var sScale = Math.min(w / srcW, h / srcH); var dW = Math.max(1, Math.round(srcW * sScale)); var dH = Math.max(1, Math.round(srcH * sScale)); var dX = Math.floor((w - dW) / 2); var dY = Math.floor((h - dH) / 2); ctx.imageSmoothingEnabled = true; ctx.drawImage(src, dX, dY, dW, dH); gif.addFrame(c, { delay: opts.delay, copy: true }); } gif.on('progress', function(p) { if (status) status.textContent = 'Building GIF: ' + Math.round(p * 100) + '%'; }); gif.on('finished', function(blob) { var firstName = items[0].name || 'images'; var base = firstName.replace(/\.[^.]+$/, ''); add_file_output(URL.createObjectURL(blob), base + '.gif'); if (status) status.textContent = 'Done.'; loaded.forEach(function(l){ try { URL.revokeObjectURL(l.url); } catch (e) {} }); }); gif.render(); }).catch(function(err) { var s = document.getElementById('i2g-status'); if (s) s.textContent = ''; alert('Could not build the animated GIF: ' + (err && err.message || err)); }); } function processFiles(files) { var batch = []; for (var i = 0; i < files.length; i++) batch.push({ blob: files[i], name: files[i].name || ('image-' + (i + 1) + '.png') }); _ctI2GRunBatch(batch); } function processFile(blob, fileName) { _ctI2GQueue.push({ blob: blob, name: fileName || 'image.png' }); if (_ctI2GTimer) clearTimeout(_ctI2GTimer); _ctI2GTimer = setTimeout(function() { var batch = _ctI2GQueue.slice(0); _ctI2GQueue = []; _ctI2GRunBatch(batch); }, 220); } var _loadedScripts = {}; function loadScriptPromise(url) { if (_loadedScripts[url]) return _loadedScripts[url]; _loadedScripts[url] = new Promise(function (resolve, reject) { var s = document.createElement('script'); s.src = url; s.onload = resolve; s.onerror = reject; document.head.appendChild(s); }); return _loadedScripts[url]; } function replaceAll(find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } function beautify(str) { var result = ''; var length = str.length; var i = 0; var braceCountLeft = 0; var braceCountRight = 0; var withinQuotes = false; while (i < length) { var c = str[i]; if (c == '"' && (i == 0 || c[i - 1] != '\\')) { // non-escaped quotes withinQuotes = !withinQuotes; } if (!withinQuotes && (c == '}' || c == '{' || c == ',')) { console.log('Start####' + result); // look back and remove carriage returns and whitespace that are already there var resultIndex = result.length - 1; while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) { resultIndex = resultIndex - 1; result = result.substr(0, resultIndex + 1); console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex); } if (c == '{') { braceCountLeft++; result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } else if (c == '}') { braceCountRight++; // precede with carriage return result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c; } else if (c == ',') { result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } var nextChar = ''; // advance through whitespace and remove carriage returns that are already there while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) { i++; } } else { result += str[i]; } i++; } return result; } function GetTabs(count) { var result = ''; for (var i = 0; i < count; i++) { result += ' '; } return result; }